package com.cuitrip.apiservice;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.date.model.ServiceDateDetail;
import com.cuitrip.business.location.model.LocationMode;
import com.cuitrip.business.tripservice.model.PictureDescription;
import com.cuitrip.business.tripservice.model.ServiceDetail;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.util.I118Util;
import com.lab.network.model.CtApiRequest;
import com.lab.network.model.CtRemoteBusiness;
import com.lab.network.model.ParseType;
import com.lab.network.model.ResultType;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.ArrayUtil;

import java.util.List;

/**
 * 行程服务HTTP请求
 */
public class ServiceBusiness {


    public static final String USER_TYPE = "user";

    public static final String SERVICE_TYPE = "service";

    /**
     * 提交行程服务
     * @param apiProxy
     * @param serviceInfo
     */
    public static void commitServiceInfo(ApiProxy apiProxy,
                                         ServiceInfo serviceInfo) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SERVICE_COMMIT);


        if (!TextUtils.isEmpty(serviceInfo.getSid())) {
            ctApiRequest.addParam("sid", serviceInfo.getSid());
        }

        //基本信息
        ctApiRequest.addParam("name", serviceInfo.getName());
        ctApiRequest.addParam("timeZone", I118Util.getTimeZone().getID());
        //选择城市
        ctApiRequest.addParam("city", serviceInfo.getAddress());
        ctApiRequest.addParam("country", serviceInfo.getCountry());
        ctApiRequest.addParam("address", serviceInfo.getAddress());
        ctApiRequest.addParam("cityAbbr", serviceInfo.getCityAbbr());

        //预约要求
        ctApiRequest.addParam("preOrderDays", serviceInfo.getPreOrderDays());
        ctApiRequest.addParam("maxbuyerNum", serviceInfo.getMaxbuyerNum());
        ctApiRequest.addParam("minbuyerNum", serviceInfo.getMinBuyerNum());
        ctApiRequest.addParam("bestTime", serviceInfo.getBestTime());
        ctApiRequest.addParam("serviceTime", serviceInfo.getServiceTime());
        ctApiRequest.addParam("lat", serviceInfo.getLat());
        ctApiRequest.addParam("lng", serviceInfo.getLng());
        ctApiRequest.addParam("meetingPlace", serviceInfo.getMeetingPlace());

        if (!TextUtils.isEmpty(serviceInfo.getMoneyType())) {
            ctApiRequest.addParam("showCurrency", serviceInfo.getMoneyType().toUpperCase());
            ctApiRequest.addParam("moneyType", serviceInfo.getMoneyType().toUpperCase());
        }
        //其他说明
        ctApiRequest.addParam("serviceSafeTips", serviceInfo.otherNotes.getServiceSafeTips());
        ctApiRequest.addParam("serviceNotice", serviceInfo.otherNotes.getServiceNotice());
        ctApiRequest.addParam("serviceWearTips", serviceInfo.otherNotes.getServiceWearTips());
        ctApiRequest.addParam("unexpectedWeather", serviceInfo.otherNotes.getUnexpectedWeather());

        //亮点描述
        ctApiRequest.addParam("brightSpot", serviceInfo.getHighLight());
        if (!ArrayUtil.isNullOrEmpty(serviceInfo.getCateList())){
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0 ; i < serviceInfo.getCateList().size(); i++) {
                if (i > 0){
                    stringBuilder.append(",");
                }
                stringBuilder.append(serviceInfo.getCateList().get(i).getCategory());
            }
            ctApiRequest.addParam("cateIds",stringBuilder.toString());
        }
        //标签已经弃用
//        ctApiRequest.addParam("serviceTags", serviceInfo.getTag());

        //图文描述
        ctApiRequest.addParam("serviceDescript", serviceInfo.getServiceDescript());
        String backPic = null;
        if (TextUtils.isEmpty(serviceInfo.getBackPic())) {
            if (!ArrayUtil.isNullOrEmpty(serviceInfo.getPictureDescriptionList())) {
                backPic = serviceInfo.getPictureDescriptionList().get(0).getUrl();
            }
        } else {
            backPic = serviceInfo.getBackPic();
        }
        ctApiRequest.addParam("serviceStory", serviceInfo.getServiceStory());

        ctApiRequest.addParam("backPic", backPic);

        if (!ArrayUtil.isNullOrEmpty(serviceInfo.getPictureDescriptionList())) {
            List<PictureDescription> pictureDescriptionList = serviceInfo.getPictureDescriptionList();
            JSONArray jsonArray = new JSONArray();
            for (PictureDescription pictureDescription : pictureDescriptionList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("descript", pictureDescription.getDescription());
                jsonObject.put("url", pictureDescription.getUrl());
                jsonArray.add(jsonObject);
            }
            ctApiRequest.addParam("picDescript", jsonArray);
        }



        //费用说明
        ctApiRequest.addParam("priceType", serviceInfo.getPriceType()); //计费方式：0 一口价;1 按人计费;2 免费
        ctApiRequest.addParam("feeInclude", serviceInfo.getFeeInclude());
        ctApiRequest.addParam("feeExclude", serviceInfo.getFeeExclude());
        ctApiRequest.addParam("price", serviceInfo.getPrice());

        //是否为草稿
        ctApiRequest.addParam("operation", serviceInfo.getOnlineState());


        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ServiceInfo.class));
    }


    public static void modifyServiceInfo(ApiProxy apiProxy,
                                         String sid, List<Long> availableDate) {

        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MODIFY_SERVICE_INFO);

        ctApiRequest.addParam("sid", sid);

        if (!ArrayUtil.isNullOrEmpty(availableDate)) {
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < availableDate.size(); i++) {
                jsonArray.add(availableDate.get(i));

            }
            ctApiRequest.addParam("availableDate", jsonArray);
        }

        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);

        apiProxy.postAsync(ctRemoteBusiness, new ParseType(JSONObject.class));
    }

    /**
     * 发现者获取服务列表
     */
    public static void getServiceList(ApiProxy apiProxy) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SERVICE_LIST);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ResultType.ARRAY, ServiceInfo.class));
    }

    public static void getServiceAvailableDate(ApiProxy apiProxy,
                                                        String sid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_AVAILABLE);

        ctApiRequest.addParam("sid", sid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);

        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ServiceDateDetail.class));
    }

    public static void getServiceAvailableAndBookedDate(ApiProxy apiProxy,
                                                        String sid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_AVAILABLE_BOOK);

        ctApiRequest.addParam("sid", sid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);

        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ServiceDateDetail.class));
    }

    public static void deleteServiceInfo(ApiProxy apiProxy,
                                                  String sid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.DELETE_SERVICE_INFO);

        ctApiRequest.addParam("sid", sid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);

        apiProxy.postAsync(ctRemoteBusiness, new ParseType(JSONObject.class));
    }

    public static void getServiceDetail(ApiProxy apiProxy,
                                                 String sid, int isDraft) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_SERVICE_DETAIL);

        ctApiRequest.addParam("sid", sid);
        ctApiRequest.addParam("isDraft", isDraft);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);

        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ServiceDetail.class));
    }


    public static void getServiceSnapShot(ApiProxy apiProxy,
                                        String orderId) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_SERVICE_SNAPSHOT);
        ctApiRequest.addParam("orderId", orderId);
        ctApiRequest.removeCurrencyParam();
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ServiceDetail.class));
    }

    public static void delPic(ApiProxy apiProxy,
                              String picUrl) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.DEL_PIC);
        ctApiRequest.addParam("picUrl", picUrl);
        ctApiRequest.removeCurrencyParam();
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void getServiceTags(ApiProxy apiProxy) {

        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_SERVICE_TAG);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ResultType.ARRAY, String.class));
    }

    public static void  getCountryCity(ApiProxy apiProxy, String country, String listType) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_COUNTRY_CITY);
        //ctApiRequest.addParam("lang", language);
        ctApiRequest.addParam("country", country);
        ctApiRequest.addParam("listType", listType);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(LocationMode.class));
    }

    public static void deleteUserAccount(ApiProxy apiProxy,String userAccountId){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.DELETE_USER_ACCOUNT);
        ctApiRequest.addParam("userAccountId", userAccountId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

}
